<?php header("Content-type: text/javascript"); ?>

/* 
 * Static ui functions here
 */

// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!

function uiInit(){
    $("#dialog").dialog("destroy");

    $("#mainFooter button[name='add']").button({
        icons: {
            primary: 'ui-icon-plus'
        },
        text: false,
    }).bind('click',function(){
        // Add form
        $("#dialog").load('dialog/addblock',
            function(){
                $('#dialog').dialog({
                    autoOpen: false,
                    height: 210,
                    width: 250,
                    modal: true,
                    title: $('#dialog').find('form').attr('title'),
                    buttons: {
                        'Create': function() {
                            var title = $("#title");
                            var type = $("#typeSelect");
                            if(title.val()!=""){
                                $.get('ajax/saveBlock/'+title.val()+'/'+type.val(),function(e){
                                    test = $('#mainContent').append(e);
                                    idArray = $(test).find('div[id^="block"]:last').attr('id');
                                    idArray = idArray.split("-");
                                    blockId = idArray[1];

                                    $("#delete-"+blockId).button({
                                        icons: {
                                            primary: 'ui-icon-trash'
                                        },
                                        text: false,
                                    }).bind('click',function(){
                                        $.post('ajax/deleteBlock/'+blockId,
                                            function(e){
                                                $("#block-"+blockId).remove();
                                        });

                                    }).css('margin-top','-3px').css({
                                        'vertical-align'    :   'middle',
                                        'float'             :   'right',
                                        'right'             :   '20px',
                                        'width'             :   '20px'
                                    })

                                    $('#block-'+blockId).draggable({
                                        handle: 'h2',
                                        containment: 'parent',
                                        snap: true,
                                        stop: function() {
                                            $.post(
                                                'ajax/setBlockPosition',
                                                {
                                                    top     :   $(this).css('top'),
                                                    left    :   $(this).css('left'),
                                                    height  :   $(this).css('height'),
                                                    width   :   $(this).css('width'),
                                                    blockId :   blockId
                                                }
                                            )}
                                        });

                                    $('#block-'+blockId).resizable({
                                        stop: function() {
                                            $.post(
                                                'ajax/setBlockPosition',
                                                {
                                                    top     :   $(this).css('top'),
                                                    left    :   $(this).css('left'),
                                                    height  :   $(this).css('height'),
                                                    width   :   $(this).css('width'),
                                                    blockId :   blockId
                                                }
                                            )}
                                    });

                                    if($('#block-'+blockId).attr('top')!="" & $('#block-'+blockId).attr('left')!=""){
                                        storedTop = parseInt($('#block-'+blockId).attr('top')) + 73;
                                        storedLeft = parseInt($('#block-'+blockId).attr('left')) + 10;

                                        $('#block-'+blockId).offset({
                                            top: storedTop,
                                            left: storedLeft
                                        });
                                    }

                                    $('#block-'+blockId).css('height', $('#block-'+blockId).attr('height')+"px");
                                    $('#block-'+blockId).css('width', $('#block-'+blockId).attr('width')+"px");
                                });
                                $(this).dialog('close');
                            } else {
                                // Uh-oh! Something went wrong. Display that now.
                                $("#dialogs").append($("#dialog").clone().attr("id","dialogError"));
                                $("#dialogError").load('dialog/error/Title+cannot+be+empty', function(){
                                    $("#dialogError").dialog({
                                        bgiframe: true,
                                        modal: true,
                                        width: 300,
                                        height: 150,
                                        autoOpen: false,
                                        title: $("#dialogError").find('div').attr('title'),
                                        buttons: {
                                            'Ok': function(){
                                                $(this).dialog('close');
                                            }
                                        }
                                    }).dialog('open');

                                    $('.ui-dialog-buttonset')
                                    .find('button:contains("Ok")')
                                    .removeClass('ui-button-text-only')
                                    .addClass('ui-button-text-icon')
                                    .prepend('<span class="ui-icon ui-icon-check"></span>')
                                    .css('width', '18px')
                                    .css('height', '20px');
                                })
                            }
                        },
                        Cancel: function() {
                                $(this).dialog('close');
                        }
                    },
                }).dialog('open');

                $('#dialog').css('visibility','visible');

                $('.ui-dialog-buttonpane')
                .find('button:contains("Create")')
                .removeClass('ui-button-text-only')
                .addClass('ui-button-text-icon')
                .prepend('<span class="ui-icon ui-icon-plus"></span>')
                .css('width', '18px')
                .css('height', '20px');

                $('.ui-dialog-buttonpane')
                .find('button:contains(Cancel)')
                .removeClass('ui-button-text-only')
                .addClass('ui-button-text-icon')
                .prepend('<span class="ui-icon ui-icon-cancel"></span>')
                .css('width', '18px')
                .css('height', '20px');
            });
    });
}